package com.soft.collateral.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soft.collateral.constant.MessageConstant;
import com.soft.collateral.entity.Collateral;
import com.soft.collateral.entity.CollateralType;
import com.soft.collateral.result.Result;
import com.soft.collateral.service.CollateralService;
import com.soft.collateral.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/7/25
 */
@RestController
@RequestMapping("/admin/collateral")
@Api(tags = "后台系统-押品接口")
@RequiredArgsConstructor
public class CollateralController {

    private final CollateralService collateralService;

    @ApiOperation("查询押品列表")
    @GetMapping("list")
    public Result list(){
        List<Collateral> collateralList = collateralService.listAll();
        if (!collateralList.isEmpty()){
            return new Result(true, MessageConstant.QUERY_COLLATERAL_SUCCESS,collateralList);
        }
        return new Result(false, MessageConstant.QUERY_COLLATERAL_FAIL);
    }

    @ApiOperation("条件分页查询押品列表")
    @GetMapping("page")
    public Result page(PageQueryVO pageQueryVO){
        Page<Collateral> pageQuery = collateralService.pageQuery(pageQueryVO);
        if (pageQuery!=null){
            return new Result(true,MessageConstant.QUERY_COLLATERAL_SUCCESS,pageQuery);
        }
        return new Result(false,MessageConstant.QUERY_COLLATERAL_FAIL);
    }

    @ApiOperation("新增押品")
    @PostMapping
    public Result create(@RequestBody Collateral collateral){
        boolean success = collateralService.saveCollateral(collateral);
        if (success){
            return new Result(true,MessageConstant.ADD_COLLATERAL_SUCCESS);
        }
        return new Result(false,MessageConstant.ADD_COLLATERAL_FAIL);
    }

    @ApiOperation("通过ID查询押品信息")
    @GetMapping("{id}")
    public Result findById(@PathVariable Integer id){
        Collateral collateral = collateralService.getByIdWithDetails(id);
        if (collateral!=null){
            return new Result(true,MessageConstant.QUERY_COLLATERAL_SUCCESS,collateral);
        }
        return new Result(true,MessageConstant.QUERY_COLLATERAL_SUCCESS);
    }

    @ApiOperation("编辑押品")
    @PutMapping
    public Result update(@RequestBody Collateral collateral){
        boolean success = collateralService.updateCollateral(collateral);
        if (success){
            return new Result(true,MessageConstant.EDIT_COLLATERAL_SUCCESS);
        }
        return new Result(false,MessageConstant.EDIT_COLLATERAL_FAIL);
    }

    @ApiOperation("删除押品")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable Integer id){
        boolean success = collateralService.removeByIdWithDetails(id);
        if (success){
            return new Result(true, MessageConstant.DELETE_COLLATERAL_SUCCESS);
        }
        return new Result(false,MessageConstant.DELETE_COLLATERAL_FAIL);
    }

    @ApiOperation("获取所有押品类型")
    @GetMapping("/types")
    public Result getCollateralTypes() {
        List<CollateralType> types = collateralService.getAllCollateralTypes();
        return new Result(true, MessageConstant.QUERY_COLLATERAL_SUCCESS, types);
    }
}
